<?php
namespace Admin\Model;
use Think\Model;
class CourseCalendarModel extends Model 
{
	protected $insertFields = array('course_id','teacher_id','classroom_id','course_date','course_time','limit_num','card_delete','color','is_show','is_daike','company_id');
	protected $updateFields = array('id','course_id','teacher_id','classroom_id','course_date','course_time','limit_num','card_delete','color','is_show','is_daike','company_id');
	protected $_validate = array();
	public function search($pageSize = 20)
	{
		/**************************************** 搜索 ****************************************/
		$where = array();
		if($course_id = I('get.course_id'))
			$where['course_id'] = array('eq', $course_id);
		if($teacher_id = I('get.teacher_id'))
			$where['teacher_id'] = array('eq', $teacher_id);
		if($classroom_id = I('get.classroom_id'))
			$where['classroom_id'] = array('eq', $classroom_id);
		if($course_date = I('get.course_date'))
			$where['course_date'] = array('eq', $course_date);
		$course_timefrom = I('get.course_timefrom');
		$course_timeto = I('get.course_timeto');
		if($course_timefrom && $course_timeto)
			$where['course_time'] = array('between', array(strtotime("$course_timefrom 00:00:00"), strtotime("$course_timeto 23:59:59")));
		elseif($course_timefrom)
			$where['course_time'] = array('egt', strtotime("$course_timefrom 00:00:00"));
		elseif($course_timeto)
			$where['course_time'] = array('elt', strtotime("$course_timeto 23:59:59"));
		if($limit_num = I('get.limit_num'))
			$where['limit_num'] = array('eq', $limit_num);
		if($card_delete = I('get.card_delete'))
			$where['card_delete'] = array('eq', $card_delete);
		if($color = I('get.color'))
			$where['color'] = array('like', "%$color%");
		if($is_show = I('get.is_show'))
			$where['is_show'] = array('eq', $is_show);
		if($is_daike = I('get.is_daike'))
			$where['is_daike'] = array('eq', $is_daike);
        //对应瑜伽馆
        if(intval(session('admin.company_id')) !==1){
            $where['company_id'] = ['eq',session('admin.company_id')];
        }
		/************************************* 翻页 ****************************************/
		$count = $this->alias('a')->where($where)->count();
		$page = new \Think\Page($count, $pageSize);
		// 配置翻页的样式
		$page->setConfig('prev', '上一页');
		$page->setConfig('next', '下一页');
		$data['page'] = $page->show();
		/************************************** 取数据 ******************************************/
		$data['data'] = $this->alias('a')->where($where)->group('a.id')->limit($page->firstRow.','.$page->listRows)->select();
		return $data;
	}
	// 添加前
	protected function _before_insert(&$data, $option)
	{
        $data['company_id'] = session('admin.company_id');
	}
	// 修改前
	protected function _before_update(&$data, $option)
	{
	}
	// 删除前
	protected function _before_delete($option)
	{
		if(is_array($option['where']['id']))
		{
			$this->error = '不支持批量删除';
			return FALSE;
		}
	}
	/************************************ 其他方法 ********************************************/
}